/**
 * @file httpServer.h
 * @author your name (you@domain.com)
 * @brief 
 * @version 0.1
 * @date 2024-07-19
 * 
 * @copyright Copyright (c) 2024
 * 
 */

#pragma once

#include <cstdint>
#include <memory>
#include <unistd.h>
#include <thread>

#include "router.h"

namespace wheels
{

/// @brief 客户端处理器
class HttpHandler;

class HttpServer
{
public:
    explicit HttpServer(std::shared_ptr<IRouter> &r);
    HttpServer(const HttpServer &) =delete;
    HttpServer & operator=(const HttpServer &) =delete;
    ~HttpServer();

    int Start(const char *address, uint16_t port);
    int Stop(void);

private:
    void closeSock()
    {
        if (m_sockFd != -1)
        {
            close(m_sockFd);
            m_sockFd = -1;
        }
    }

    int createListenSock(const char *address, uint16_t port);
    void OnAccept(int sockFd); 

private:
    std::thread m_thread;
    std::unique_ptr<HttpHandler> m_clientHandler;
    std::shared_ptr<IRouter> m_router;
    int m_sockFd = -1;
    bool m_looping = true;
};

} //< wheels
